package com.fulihui.wgj.admin.weixin.impl;

import com.fulihui.weixinsdk.WeixinSDK;
import com.fulihui.weixinsdk.request.user.UserInfoGetRequest;
import com.fulihui.weixinsdk.result.user.UserInfoGetResult;
import com.fulihui.wgj.admin.integration.WxPlatformClient;
import com.fulihui.wgj.admin.weixin.WeiXinUserManage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: Levon
 * @date: 2018-07-31 09:48
 */
@Service
public class WeiXinUserManageImpl implements WeiXinUserManage {

    private final static Logger LOGGER = LoggerFactory.getLogger(WeiXinUserManageImpl.class);

    @Autowired
    private WxPlatformClient wxPlatformClient;

    @Autowired
    private WeixinSDK weixinSDK;

    @Override
    public UserInfoGetResult getUserDetail(String appId, String openId) {
        String accessToken = wxPlatformClient.queryTokenFromRedis(appId);
        if (StringUtils.isBlank(accessToken)) {
            LOGGER.info("GetUserDetail!!get token is null");
            return null;
        }
        UserInfoGetRequest userGetRequest = new UserInfoGetRequest();
        userGetRequest.setAccess_token(accessToken);
        userGetRequest.setOpenid(openId);
        UserInfoGetResult userInfoGetResult = weixinSDK.invokeService(userGetRequest);
        if (userInfoGetResult != null && userInfoGetResult.isSuccess()) {
            return userInfoGetResult;
        }
        return null;
    }
}
